package main

import (
	"encoding/xml"
	"fmt"
	"io/ioutil"
	"os"
)

type EmailConfig struct {
	XMLName        xml.Name       `xml:"config"`   //xml文件根标签名
	SmtpServer     string         `xml:smtpServer` //一级子标签名
	SmtpPort       int            `xml:smtpPort`
	Sender         string         `xml:"sender"`
	SenderPassword string         `xml:senderPassword`
	Receivers      EmailReceivers `xml:"receivers"` //子标签具有子结构，新定义结构图
}

type EmailReceivers struct {
	Flag string   `xml:"flag,attr"` //标签属性名
	User []string `xml:"user"`
}

func main() {
	fp, err := os.Open("cp6_file/xml/default.xml")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer fp.Close()
	//将xml读取为字节数组
	data, err := ioutil.ReadAll(fp)
	if err != nil {
		fmt.Println(err)
		return
	}
	xmlObject := EmailConfig{}

	err = xml.Unmarshal(data, &xmlObject) //解析xml文件，转为结构体对象
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(xmlObject)
	for _, v := range xmlObject.Receivers.User {
		fmt.Println(v)
	}
}
